using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;


namespace syntaxChecker
{
    class syntaxCheck
    {        
        static public void checkSyntax(StreamReader codeStream)
        {
            Regex expRtype = new Regex(@"(add|sub|and|slt|ADD|SUB|AND|SLT)(\s)+([\d]|1[0-5])(\s)*,(\s)*([\d]|1[0-5])(\s)*,(\s)*([\d]|1[0-5]){1,2}", RegexOptions.IgnoreCase);
            Regex expImmtype = new Regex(@"(addi|beq|bne)(\s)+((\d)|1[0-5])(\s)*,(\s)*((\d)|1[0-5])(\s)*,(\s)*((\-)?((\d)(\d)?(\d)?(\d)?|[1-2](\d)(\d)(\d)(\d)|3[0-1](\d)(\d)(\d)|32[0-6](\d)(\d)|327[0-5](\d))|(\-)3276[0-8]|3276[0-7]{1,2,3,4,5})", RegexOptions.IgnoreCase);
            Regex expJump=new Regex(@"j((\d)(\d)?(\d)?(\d)?|[1-2](\d)(\d)(\d)(\d)|3[0-1](\d)(\d)(\d)|32[0-6](\d)(\d)|327[0-5](\d)|3276[0-8]){1,2,3,4,5}",RegexOptions.IgnoreCase);
            Regex expLSW = new Regex(@"(lw|sw)(\s)+((\d)|1[0-5])(\s)*,(\s)*((\-)?((\d)(\d)?(\d)?(\d)?|[1-2](\d)(\d)(\d)(\d)|3[0-1](\d)(\d)(\d)|32[0-6](\d)(\d)|327[0-5](\d))|(\-)3276[0-8]|3276[0-7])(\s)*(\()(\s)*((\d)|1[0-5])(\))", RegexOptions.IgnoreCase);

            string codeLine;
            int lineCounter = 0,u=0;
            int[] erronousLines = new int[100];
            bool error = false;


            while (!codeStream.EndOfStream)
            {
                codeLine = codeStream.ReadLine();
                lineCounter++;
                Match M_Rtype = expRtype.Match(codeLine);
                Match M_Immtype = expImmtype.Match(codeLine);
                Match M_Jump=expJump.Match(codeLine);
                Match M_LSW=expLSW.Match(codeLine);
                
                
                
                if (M_Rtype.Success)
                {
                    Console.WriteLine(M_Rtype.Value);
                }
                else if (M_Immtype.Success)
                {
                    Console.WriteLine(M_Immtype.Value);
                }
                else if (M_Jump.Success)
                {
                    Console.WriteLine(M_Jump.Value);
                }
                else if (M_LSW.Success)
                {
                    Console.WriteLine(M_LSW.Value);
                }
                else
                {
                    error = true;
                    erronousLines[u]=lineCounter;
                    u++;
                }

            }
            
            if (error)
            {
                Console.Write("Syntax Error! check the following lines--> ");
                for (int i = 0; i < u; i++)
                {
                    if (i != u - 1)
                    {
                        Console.Write("{0}, ", erronousLines[i]);
                    }
                    else
                    {
                        Console.WriteLine("{0}", erronousLines[i]);
                    }
                }
            }
            
        }

    }
}